﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using wps_demo.Models;
using wps_demo.Utils;

namespace wps_demo.Controller
{
    public class OauthController : ApiController
    {
        [HttpGet, Route("weboffice/url")]
        public JsonResult<WPS_Url> GetAppToken()
        {
            LogHelper.Info("进入weboffice/url");
            string filename = string.Empty;
            HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
            if (httpContextBase == null)
            {
                return null;
            }
            HttpRequestBase request = httpContextBase.Request;
            filename = request["filename"].ToString();
            LogHelper.Info("文件名：" + filename);
            if (string.IsNullOrEmpty(filename))
            {
                return null;
            }
            string url = WPS_Config.Domain + "/office";
            if (filename.EndsWith("xls"))
            {
                url += "/s/2?";
            }
            else if (filename.EndsWith("ppt"))
            {
                url += "/p/3?";
            }
            else if (filename.EndsWith("pdf"))
            {
                url += "/f/4?";
            }
            else
            {
                url += "/w/1?";
            }
            string param = "_w_appid=" + WPS_Config.AppId + "&_w_fname=" + filename;
            if (request["file_permission"] != null)
            {
                param += "&_w_permission=" + request["file_permission"].ToString();
            }
            LogHelper.Info("param：" + param);
            //   param = "_w_appid=123456&_w_fname=222.docx&_w_userid=id1000";
            String signature = WpsUtils.getSignature(WpsUtils.paramToMap(param), WPS_Config.AppKey);
            url += param + "&_w_signature=" + signature;
            WPS_Url urlModel = new WPS_Url();
            urlModel.Url = url;
            urlModel.Token = "1";
            LogHelper.Info("token：" + urlModel.Token + ";url：" + urlModel.Url);
            LogHelper.Info("离开weboffice/url");
            return Json(urlModel);
        }

        [HttpGet,Route("weboffice/new")]
        public JsonResult<JObject> NewFile()
        {
            try
            {
                LogHelper.Info("进入weboffice/new");
                JObject obj = new JObject();
                string param = "_w_appid=" + WPS_Config.AppId;
                String signature = WpsUtils.getSignature(WpsUtils.paramToMap(param), WPS_Config.AppKey);
                string url = WPS_Config.Domain + "/office/w/new/0?";
                url += param + "&_w_signature=" + signature;
                obj["redirect_url"] = url;
                obj["user_id"] = "wangwc";
                LogHelper.Info("redirect_url："+url);
                LogHelper.Info("离开weboffice/new");
                return Json(obj);
            }
            catch (Exception e)
            {
                LogHelper.Error(e.Message);
            }
            return null;
        }

    }
}
